﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Sample1ViewModel.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   The main view model
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Samples.Commands
{
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel.DataAnnotations;
    using System.Diagnostics;
    using System.Threading;
    using System.Windows;
    using System.Windows.Input;
    using ComponentModel.Modelling;
    using Threading;

    /// <summary>
    /// The main view model
    /// </summary>
    public class Sample1ViewModel : ModelBase
    {
        #region Fields

        private int _longRunningTaskProgress;
        private ObservableCollection<Sample1Person> _people;
        private bool _canPerformLongRunningAction;

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="Sample1ViewModel"/> class.
        /// </summary>
        public Sample1ViewModel()
        {
            People.Add(new Sample1Person("Young", "Jason"));
            People.Add(new Sample1Person("Smith", "James"));
            CanPerformLongRunningAction = true;
        }

        /// <summary>
        /// Gets the people.
        /// </summary>
        /// <value>The people.</value>
        public ObservableCollection<Sample1Person> People
        {
            get { return _people ?? (_people = new ObservableCollection<Sample1Person>()); }
        }

        /// <summary>
        /// Undoes the changes.
        /// </summary>
        /// <param name="person">The person.</param>
        /// <param name="firstName">The first name.</param>
        /// <param name="surname">The surname.</param>
        [Display(Name = "Undo Changes")]
        public void UndoChanges(Sample1Person person, string firstName, string surname)
        {
            if (CanUndoChanges(person, firstName, surname))
            {
                person.RaisePropertyChanged(string.Empty);
            }
        }

        /// <summary>
        /// Determines whether this instance can undo changes to the specified person.
        /// </summary>
        /// <param name="person">The person.</param>
        /// <param name="firstName">The first name.</param>
        /// <param name="surname">The surname.</param>
        /// <returns>
        /// <c>true</c> if this instance can undo changes to the specified person; otherwise, <c>false</c>.
        /// </returns>
        public bool CanUndoChanges(Sample1Person person, string firstName, string surname)
        {
            return person != null && (person.Firstname != firstName || person.Surname != surname);
        }

        /// <summary>
        /// Performs a long running action.
        /// </summary>
        public void PerformLongRunningAction()
        {
            if (CanPerformLongRunningAction)
            {
                CanPerformLongRunningAction = false;
                LongRunningTaskProgress = 0;
                ThreadPool.QueueUserWorkItem(x =>
                                                 {
                                                     for (var i = 0; i < 20; i++)
                                                     {
                                                         UIDispatcher.Current.BeginInvoke(
                                                             () => LongRunningTaskProgress += 5);
                                                         Thread.Sleep(500);
                                                     }
                                                     UIDispatcher.Current.BeginInvoke(
                                                         () => CanPerformLongRunningAction = true);
                                                 }, null);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance can perform long running action.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance can perform long running action; otherwise, <c>false</c>.
        /// </value>
        public bool CanPerformLongRunningAction
        {
            get { return _canPerformLongRunningAction; }
            protected set { SetProperty(ref _canPerformLongRunningAction, value, "CanPerformLongRunningAction"); }
        }

        /// <summary>
        /// Gets or sets the long running task progress.
        /// </summary>
        /// <value>The long running task progress.</value>
        public int LongRunningTaskProgress
        {
            get { return _longRunningTaskProgress; }
            set { SetProperty(ref _longRunningTaskProgress, value, "LongRunningTaskProgress"); }
        }

        /// <summary>
        /// Shows the details.
        /// </summary>
        /// <param name="person">The person.</param>
        public void ShowCurrentDetails(Sample1Person person)
        {
            MessageBox.Show("Current Details: " + person);
        }

        /// <summary>
        /// Determines whether this instance [can show details] the specified person.
        /// </summary>
        /// <param name="person">The person.</param>
        public bool CanShowCurrentDetails(Sample1Person person)
        {
            return person != null;
        }

        public void SendKeys(Key key)
        {
            Console.WriteLine(key);
        }
    }
}
